package cn.gl.org.service.impl;

import cn.gl.basic.util.BaiduAuditUtils;
import cn.gl.basic.util.Md5Utils;
import cn.gl.basic.util.StrUtils;
import cn.gl.org.domain.Employee;
import cn.gl.org.domain.Shop;
import cn.gl.org.domain.ShopAuditLog;
import cn.gl.org.dto.ShopDto;
import cn.gl.org.mapper.EmployeeMapper;
import cn.gl.org.mapper.ShopAuditLogMapper;
import cn.gl.org.mapper.ShopMapper;
import cn.gl.org.service.IShopService;
import cn.gl.basic.service.impl.BaseServiceImpl;
import cn.gl.usr.domain.Logininfo;
import cn.gl.usr.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void settlement(Shop shop) {
        //一.校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getName())||StringUtils.isEmpty(shop.getTel())){
            throw new RuntimeException("信息不能为空");
        }
        //2.两次密码不一致校验
        if (!shop.getAdmin().getComfirmPassword().equals(shop.getAdmin().getPassword())){
            throw new RuntimeException("两次密码不一致");
        }
        //3.当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if (dbShop != null) {//入驻过
            throw new RuntimeException("当前店铺已存在，请直接登录。找回密码请联系管理员");

        }
        //二.百度ai智能审核
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new RuntimeException("店铺名称不合法!!!");
        }
        if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())) {
            throw new RuntimeException("logo不合法!!!");
        }
        //三：添加到数据库
        //1.添加员工信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(1);//可以登陆
        logininfoMapper.insert(logininfo);

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.insert(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.insert(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShop_id(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);

        //四：人工审核之后发送激活邮件
    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    public void auditpass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 - 2审核通过待激活【1.先查再改 2.直接修改】
        shopMapper.updateState(2,log.getShopId());
        //2添加审核日志
        log.setState(1);//审核通过
        log.setAuditId(350L);
        shopAuditLogMapper.insert(log);
        //发邮件给店长通知他激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        //发件人信息
        helper.setFrom("1578203936@qq.com");
        //邮件主题
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.laodOne(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208/group1/"+shop.getLogo()+"' ><a href='http://127.0.0.1:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>");
        //收件人
        Employee employee = employeeMapper.laodOne(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);

    }
    //店铺激活
    @Override
    public void activation(Long shopId) {
        //根据取到的id 查询id下的店铺信息
        Shop shop = shopMapper.laodOne(shopId);
        //店铺状态设置激活
        shop.setState(3);
        //修改店铺状态
        shopMapper.update(shop);

    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    /**
     * 审核驳回
     * @param log
     */
    @Override
    public void auditreject(ShopAuditLog log) {
        //3.发邮件给店长通知他重新修改信息入驻
        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom("1578203936@qq.com");
        message.setSubject("店铺审核失败通知");
        //true-能够在内容组编写html标签 会解析
        Shop shop = shopMapper.laodOne(log.getShopId());
        message.setText("<h1>店铺审核失败，请重新入驻</h>");
        //收件人
        Employee employee = employeeMapper.laodOne(shop.getAdminId());
        message.setTo(employee.getEmail());//改邮件是店长的邮件
        javaMailSender.send(message);
    }
}